package com.zjh.common.util;

import com.zjh.admin.sys.model.ResourceEntity;
import com.zjh.common.model.JSTreeEntity;
import com.zjh.common.model.Select2Entity;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * @author lk
 * @Description: 树形结构工具类
 * @date 2016/2/18 16:59
 */
public class TreeUtil {

    /**
     * select2下拉组件数据对象
     */
    private List<Select2Entity> selectTree = new ArrayList<>();

    /**
     * 生成select2下拉组件数据时遍历的次数
     */
    private int selectCnt = 0;

    /**
     * 生成jstree要求的数据结构
     *
     * @param resourceList
     * @return
     */
    public List<JSTreeEntity> generateJSTree(List<ResourceEntity> resourceList) {
        List<JSTreeEntity> jstreeList = new ArrayList<>();

        for (ResourceEntity resourceEntity : resourceList) {
            JSTreeEntity jstree = new JSTreeEntity();

            jstree.setId(resourceEntity.getId().toString());
            jstree.setParent(resourceEntity.getParentId() == null ? "#" : resourceEntity.getParentId().toString());
            jstree.setText(resourceEntity.getName());
            jstree.setIcon(StringUtils.isBlank(resourceEntity.getIcon()) ? "fa fa-cog" : resourceEntity.getIcon());

            JSTreeEntity.State state = new JSTreeEntity().new State();
            state.setDisabled(false);
            state.setSelected(resourceEntity.getSelected());
            state.setOpened(true);

            jstree.setState(state);
            jstreeList.add(jstree);
        }

        return jstreeList;
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     具有树形结构特点的集合
     * @param parentId 父节点ID
     * @return 树形结构集合
     */
    public List<Select2Entity> getSelectTree(List<ResourceEntity> list, Integer parentId) {
        List<ResourceEntity> returnList = getChildResourceEntitys(list, parentId);
        recursionForSelect(returnList);
        return selectTree;
    }


    /**
     * 递归列表
     *
     * @param list
     */
    private void recursionForSelect(List<ResourceEntity> list) {
        String str = "";
        for (int i = 0; i < selectCnt; i++) {
            str += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        for (ResourceEntity re : list) {
            if (null == re.getParentId()) {
                str = "";
                selectCnt = 0;
            }
            Select2Entity se = new Select2Entity();
            se.setId(re.getId().toString());
            se.setText(str + re.getName());
            se.setName(re.getName());
            selectTree.add(se);
            if (re.getChildren().size() > 0) {
                selectCnt++;
                recursionForSelect(re.getChildren());
            }
        }
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     具有树形结构特点的集合
     * @param parentId 父节点ID
     * @return 树形结构集合
     */
    public static List<ResourceEntity> getChildResourceEntitys(List<ResourceEntity> list, Integer parentId) {
        List<ResourceEntity> returnList = new ArrayList<>();
        for (Iterator<ResourceEntity> iterator = list.iterator(); iterator.hasNext(); ) {
            ResourceEntity t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (null == parentId ? null == t.getParentId() : parentId.equals(t.getParentId())) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private static void recursionFn(List<ResourceEntity> list, ResourceEntity t) {
        List<ResourceEntity> childList = getChildList(list, t);// 得到子节点列表
        t.setChildren(childList);
        for (ResourceEntity tChild : childList) {
            if (hasChild(list, tChild)) {// 判断是否有子节点
                Iterator<ResourceEntity> it = childList.iterator();
                while (it.hasNext()) {
                    ResourceEntity n = (ResourceEntity) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     *
     * @param list
     * @param t
     * @return
     */
    private static List<ResourceEntity> getChildList(List<ResourceEntity> list, ResourceEntity t) {
        List<ResourceEntity> tlist = new ArrayList<ResourceEntity>();
        Iterator<ResourceEntity> it = list.iterator();
        while (it.hasNext()) {
            ResourceEntity n = (ResourceEntity) it.next();
            if (t.getType() != 2) {
                if (null == n.getParentId() ? null == Integer.valueOf(t.getId().toString()) : n.getParentId().equals(Integer.valueOf(t.getId().toString()))) {
                    //n.setParentName(t.getName());
                    tlist.add(n);
                }
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     *
     * @param list
     * @param t
     * @return
     */
    private static boolean hasChild(List<ResourceEntity> list, ResourceEntity t) {
        return getChildList(list, t).size() > 0;
    }

}
